#!/usr/bin/env bash
set -e

RUBBERBAND_SRC=$(cd $(dirname $0)/../rubberband && pwd)
BUILD_DIR=$(cd $(dirname $0)/../build && pwd)

# 编译 arm64 + x86_64 两个架构 (armv7a 暂时跳过)
ARCHS="arm64 x86_64"

for ARCH in $ARCHS; do
    echo "== Building rubberband for $ARCH =="
    OUT_DIR=$BUILD_DIR/rubberband-$ARCH
    mkdir -p $OUT_DIR

    # NDK r10e 使用不同的工具链结构
    case $ARCH in
        armv7a)
            TOOLCHAIN_PREFIX="$ANDROID_NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64"
            export CC="$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi-gcc"
            export CXX="$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi-g++"
            export AR="$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi-ar"
            export LD="$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi-ld"
            export STRIP="$TOOLCHAIN_PREFIX/bin/arm-linux-androideabi-strip"
            HOST=arm-linux-androideabi
            MESON_CPU_FAMILY=arm
            MESON_CPU=armv7
            SYSROOT="$ANDROID_NDK/platforms/android-21/arch-arm"
            NDK_ARCH=armeabi-v7a
            ;;
        arm64)
            TOOLCHAIN_PREFIX="$ANDROID_NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64"
            export CC="$TOOLCHAIN_PREFIX/bin/aarch64-linux-android-gcc"
            export CXX="$TOOLCHAIN_PREFIX/bin/aarch64-linux-android-g++"
            export AR="$TOOLCHAIN_PREFIX/bin/aarch64-linux-android-ar"
            export LD="$TOOLCHAIN_PREFIX/bin/aarch64-linux-android-ld"
            export STRIP="$TOOLCHAIN_PREFIX/bin/aarch64-linux-android-strip"
            HOST=aarch64-linux-android
            MESON_CPU_FAMILY=aarch64
            MESON_CPU=aarch64
            SYSROOT="$ANDROID_NDK/platforms/android-21/arch-arm64"
            NDK_ARCH=arm64-v8a
            ;;
        x86)
            TOOLCHAIN_PREFIX="$ANDROID_NDK/toolchains/x86-4.9/prebuilt/linux-x86_64"
            export CC="$TOOLCHAIN_PREFIX/bin/i686-linux-android-gcc"
            export CXX="$TOOLCHAIN_PREFIX/bin/i686-linux-android-g++"
            export AR="$TOOLCHAIN_PREFIX/bin/i686-linux-android-ar"
            export LD="$TOOLCHAIN_PREFIX/bin/i686-linux-android-ld"
            export STRIP="$TOOLCHAIN_PREFIX/bin/i686-linux-android-strip"
            HOST=i686-linux-android
            MESON_CPU_FAMILY=x86
            MESON_CPU=i686
            SYSROOT="$ANDROID_NDK/platforms/android-21/arch-x86"
            ;;
        x86_64)
            TOOLCHAIN_PREFIX="$ANDROID_NDK/toolchains/x86_64-4.9/prebuilt/linux-x86_64"
            export CC="$TOOLCHAIN_PREFIX/bin/x86_64-linux-android-gcc"
            export CXX="$TOOLCHAIN_PREFIX/bin/x86_64-linux-android-g++"
            export AR="$TOOLCHAIN_PREFIX/bin/x86_64-linux-android-ar"
            export LD="$TOOLCHAIN_PREFIX/bin/x86_64-linux-android-ld"
            export STRIP="$TOOLCHAIN_PREFIX/bin/x86_64-linux-android-strip"
            HOST=x86_64-linux-android
            MESON_CPU_FAMILY=x86_64
            MESON_CPU=x86_64
            SYSROOT="$ANDROID_NDK/platforms/android-21/arch-x86_64"
            NDK_ARCH=x86_64
            ;;
    esac

    export SYSROOT
    # 添加 C++ 标准库包含路径
    STL_INCLUDE="$ANDROID_NDK/sources/cxx-stl/gnu-libstdc++/4.9/include"
    STL_ARCH_INCLUDE="$ANDROID_NDK/sources/cxx-stl/gnu-libstdc++/4.9/libs/$NDK_ARCH/include"
    STL_LIBS="$ANDROID_NDK/sources/cxx-stl/gnu-libstdc++/4.9/libs/$NDK_ARCH"
    
    export CFLAGS="--sysroot=$SYSROOT"
    export CXXFLAGS="--sysroot=$SYSROOT -I$STL_INCLUDE -I$STL_ARCH_INCLUDE"
    export LDFLAGS="--sysroot=$SYSROOT -L$STL_LIBS -lgnustl_static"
    
    # 为 ARM 架构添加特定的编译标志
    if [ "$ARCH" = "armv7a" ]; then
        export CFLAGS="$CFLAGS -march=armv7-a -mfpu=neon -mfloat-abi=softfp"
        export CXXFLAGS="$CXXFLAGS -march=armv7-a -mfpu=neon -mfloat-abi=softfp"
    fi

    # 创建 meson 交叉编译文件
    CROSS_FILE="$RUBBERBAND_SRC/cross-$ARCH.txt"
    cat > "$CROSS_FILE" << EOF
[binaries]
c = '$CC'
cpp = '$CXX'
ar = '$AR'
ld = '$LD'
strip = '$STRIP'
pkgconfig = 'pkg-config'

[host_machine]
system = 'android'
cpu_family = '$MESON_CPU_FAMILY'
cpu = '$MESON_CPU'
endian = 'little'

[built-in options]
c_args = ['--sysroot=$SYSROOT']
cpp_args = ['--sysroot=$SYSROOT', '-I$STL_INCLUDE', '-I$STL_ARCH_INCLUDE']
c_link_args = ['--sysroot=$SYSROOT']
cpp_link_args = ['--sysroot=$SYSROOT', '-L$STL_LIBS', '-lgnustl_static']
EOF

    cd $RUBBERBAND_SRC
    
    # 清理之前的构建
    rm -rf build-$ARCH
    
    # 使用 meson 构建，指定使用内置重采样器和 FFT
    meson setup build-$ARCH \
        --cross-file="$CROSS_FILE" \
        --prefix="$OUT_DIR" \
        --default-library=static \
        -Dresampler=builtin \
        -Dfft=builtin \
        -Dbuildtype=release
    
    cd build-$ARCH
    ninja
    ninja install
    
    # 创建 pkg-config 文件
    mkdir -p "$OUT_DIR/lib/pkgconfig"
    cat > "$OUT_DIR/lib/pkgconfig/rubberband.pc" << EOF
prefix=$OUT_DIR
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: rubberband
Version: 3.1.0
Description: Audio time-stretching and pitch-shifting library
Libs: -L\${libdir} -lrubberband
Cflags: -I\${includedir}
EOF
    
    cd -
done 